package com.gitee.wsl.jvm.ext.store

import java.io.ByteArrayOutputStream
import java.io.InputStream
import java.io.OutputStream
import java.util.zip.ZipEntry
import java.util.zip.ZipInputStream
import java.util.zip.ZipOutputStream

fun InputStream.readAsBytes(): ByteArray? {
    ByteArrayOutputStream().use { byteArrayOutputStream ->
        val byteArray = ByteArray(2048)
        while (true) {
            val count = this.read(byteArray, 0, 2048)
            if (count <= 0) {
                break
            } else {
                byteArrayOutputStream.write(byteArray, 0, count)
            }
        }
        return byteArrayOutputStream.toByteArray()
    }
}

fun InputStream.zipInputStream(): ZipInputStream = ZipInputStream(this)

fun OutputStream.zipOutputStream(): ZipOutputStream = ZipOutputStream(this)

fun InputStream.zipInputStream(block:(ZipInputStream)->Unit) = ZipInputStream(this).use(block)

fun OutputStream.zipOutputStream(block:(ZipOutputStream)->Unit) = ZipOutputStream(this).use(block)

fun ZipOutputStream.addEntry(out: ZipEntry,inputStream: InputStream){
    putNextEntry(out)
    inputStream.copyTo(this)
}

fun ZipOutputStream.addEntry(out: String,inputStream: InputStream) = addEntry(ZipEntry(out),inputStream)
